package com.example.yls.demoa;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

        private MyBroadcastReceiver receiver;
        private Button btnSend;
        private Button btnSendTrafic;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            receiver = new MyBroadcastReceiver();

            IntentFilter filter=new IntentFilter();
            filter.addAction("Weather_Report");
            filter.addAction("Trafic_Report");

            registerReceiver(receiver,filter);

            btnSend =findViewById(R.id.btn_send);

            btnSend.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent =new Intent();
                    intent.setAction("Weather_Report");
                    intent.putExtra("Weather","sunny");
                    intent.putExtra("temprature",25);
                    sendBroadcast(intent);
                }
            });
            btnSendTrafic = findViewById(R.id.btn_send_trafic);

            btnSendTrafic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent =new Intent();
                    intent.setAction("Trafic_Report");
                    intent.putExtra("Trafic","堵车");
                    intent.putExtra("Time",40);
                    sendBroadcast(intent);
                }
            });
        }

        @Override
        protected void onDestroy() {
            unregisterReceiver(receiver);
            super.onDestroy();
        }
    }

